package lc20240514;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}
public class Demo1 {
     public static void main(String[] args) {
         TreeNode root = new TreeNode(1);
         root.left = new TreeNode(2);
         root.right = new TreeNode(3);
//         root.left.left = new TreeNode(4);
         root.left.right = new TreeNode(5);

         ArrayList<Integer> result = PrintFromTopToBottom(root);

         for (Integer in : result) {
             System.out.println(in);
         }
    }

    public static ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {

        ArrayList<Integer> list=new ArrayList<>();
        if (root==null){
            return null;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode node=queue.poll();
            list.add(node.val);
            if (node.left!=null&&root.left.val!='#'){
                queue.add(node.left);
            }
            if (node.right!=null&&root.right.val!='#'){
                queue.add(node.right);
            }
        }
        return list;
    }
}
